package com.leenmvc.core.web.controller;

import com.leenmvc.core.annotation.Note;
import com.leenmvc.core.base.BaseController;
import com.leenmvc.core.exception.BusinessException;
import com.leenmvc.core.security.User;
import com.leenmvc.core.security.entity.core.SysMenu;
import com.leenmvc.core.security.entity.core.SysRole;
import com.leenmvc.core.security.service.core.SysMenuService;
import com.leenmvc.core.security.service.core.SysRoleService;
import com.leenmvc.core.utils.collection.TreeUtils;
import com.leenmvc.core.base.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统菜单控制器
 * @author zhousc
 * @date 2020年7月11日
 */
@RestController
@RequestMapping("system/sysRole")
public class SysRoleController extends BaseController<SysRole> {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("list")
    @Note("获取所有角色列表，展示成树结构")
    public Result list() {
        // 获取当前用户角色及之下的角色
        Long roleId = User.getRoleId();
        List<SysRole> roles = listBySqlKey("getList","id",roleId);
        for (SysRole role : roles) {
            Long topId = TreeUtils.findTopId(role);
        }
        List<SysRole> build = TreeUtils.build(roles);
        return new Result().ok(build);
    }

    @GetMapping("roleAddUserList")
    @Note("点开新增用户按钮，回显页面数据")
    public Result roleAddUserList(@RequestParam Long roleId) {
        List list = sysRoleService.roleAddUserList(roleId);
        return new Result().ok(list);
    }


    @PutMapping("updateRoleUser")
    @Note("修改用户与角色关系，传入用户id列表")
    public Result updateRoleUser(@RequestParam Long roleId, @RequestBody List<String> userIds) {
        sysRoleService.updateRoleUser(roleId, userIds);
        return new Result();
    }

    /**
     *
     * @param parentRoleId 父角色id
     * @param roleId       当前角色id
     * @return
     */
    @GetMapping("getInitMenuPermission")
    @Note("新增或修改显示对应菜单列表(回显)")
    public Result getInitMenuPermission(@RequestParam Long parentRoleId, @RequestParam(required = false) Long roleId){
        // 获取父角色对应的菜单
        List<SysMenu> menuListParent = sysMenuService.getMenuListForRole(parentRoleId);
        Map<Long,SysMenu> map = new HashMap<>();
        // 把父节点列表放入map中,并把所有节点状态至0
        if (menuListParent.size() > 0){
            menuListParent.forEach(sysMenu -> {
                sysMenu.setSign(0);
                map.put(sysMenu.getId(),sysMenu);
            });
        }else {
            throw new BusinessException("父级角色无菜单,请重新选择");
        }
        // 获取当前角色已拥有的菜单,新增没有roleid
        List<SysMenu> menuListSub = null;
        if (roleId != null){
            menuListSub = sysMenuService.getMenuListForRole(roleId);

        }

        if (menuListSub != null && menuListSub.size() > 0){
            // 遍历子节点list,判断是否存在map中
            menuListSub.forEach(sysMenu -> {
                SysMenu mapMenu = map.get(sysMenu.getId());
                mapMenu.setSign(1);
            });

            // 在将map转化成list
            menuListParent.clear();
            map.keySet().forEach(id->{
                menuListParent.add(map.get(id));
            });
        }
        for (SysMenu menu : menuListParent) {
            Long topId = TreeUtils.findTopId(menu);
            menu.setTopId(topId);
        }
        List<SysMenu> build = TreeUtils.build(menuListParent);

        return new Result().ok(build);
    }
}
